package com.minsu.mapper;

import com.minsu.entity.Fuel;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Mapper
public interface FuelMapper {

    //    @Select("select * from furnace_materials order by updated_at desc")
//    List<Fuel> findAllFuel();
//    @Select("select f.*,s.supplier_name from  furnace_materials f  left join  suppliers s on f.supplier_id = s.supplier_id  order by f.updated_at desc limit #{offset}, #{pageSize}")
//    List<Fuel> findAllFuel(@Param("offset") int offset, @Param("pageSize") int pageSize);

    //    @Insert("insert into furnace_materials(batch_no, material_type, supplier_id, stock_quantity, price, in_time," +
//            "    carbon_content, silicon_content, manganese_content, phosphorus_content, sulfur_content)" +
//            " values (#{batchNo},#{materialType},#{supplierId},#{stockQuantity},#{price},#{inTime},#{carbonContent}" +
//            ",#{siliconContent},#{manganeseContent},#{phosphorusContent},#{sulfurContent})")
    @Insert(
            "INSERT INTO furnace_materials (" +
                    "  batch_no, material_type, supplier_id, stock_quantity, price, in_time, out_time," +
                    "  carbon_content, silicon_content, manganese_content, phosphorus_content," +
                    "  sulfur_content, magnesium_content, rare_earth_content," +
                    "  created_at, updated_at" +
                    ") VALUES (" +
                    "  #{batchNo}, #{materialType}, #{supplierId}, #{stockQuantity}, #{price}, #{inTime}, #{outTime}," +  // 注意这里的逗号
                    "  #{carbonContent}, #{siliconContent}, #{manganeseContent}, #{phosphorusContent}," +
                    "  #{sulfurContent}, #{magnesiumContent}, #{rareEarthContent}," +
                    "  NOW(), NOW()" +
                    ")")
    void addFuel(Fuel fuel);


    // 添加批量更新炉料出库时间的方法
    @Update("<script>" +
            "UPDATE furnace_materials " +
            "SET out_time = #{productionTime} " +
            "WHERE batch_no IN " +
            "<foreach item='batch' collection='batchNos' open='(' separator=',' close=')'>" +
            "#{batch}" +
            "</foreach>" +
            "</script>")
    void updateOutTimeByBatchNos(@Param("batchNos") List<String> batchNos,
                                 @Param("productionTime") Date productionTime);


    @Delete("delete from furnace_materials where batch_no = #{batchNo}")
    void deleteById(String batchNo);

    //
//    @Select("select * from  furnace_materials where batch_no = #{batchNo} order by updated_at desc")
    @Results({
            @Result(property = "supplierName", column = "supplier_name")
    })
    @Select("SELECT f.*, s.supplier_name " +
            "FROM furnace_materials f " +
            "LEFT JOIN suppliers s ON f.supplier_id = s.supplier_id " +
            "WHERE f.batch_no = #{batchNo}")
    Fuel findByBatchNo(String batchNo);

    @Update("<script>" +
            "UPDATE furnace_materials " +
            "<set>" +
            "<if test='materialType != null'>material_type = #{materialType},</if>" +
            "<if test='supplierId != null'>supplier_id = #{supplierId},</if>" +
            "<if test='stockQuantity != null'>stock_quantity = #{stockQuantity},</if>" +
            "<if test='price != null'>price = #{price},</if>" +
            "<if test='inTime != null'>in_time = #{inTime},</if>" +
            "<if test='carbonContent != null'>carbon_content = #{carbonContent},</if>" +
            "<if test='siliconContent != null'>silicon_content = #{siliconContent},</if>" +
            "<if test='manganeseContent != null'>manganese_content = #{manganeseContent},</if>" +
            "<if test='phosphorusContent != null'>phosphorus_content = #{phosphorusContent},</if>" +
            "<if test='sulfurContent != null'>sulfur_content = #{sulfurContent},</if>" +
            "<if test='magnesiumContent != null'>magnesium_content = #{magnesiumContent},</if>" +
            "<if test='rareEarthContent != null'>rare_earth_content = #{rareEarthContent},</if>" +
            "updated_at = NOW()" +
            "</set>" +
            "WHERE batch_no = #{batchNo}" +
            "</script>")
    void updateFuel(Fuel fuel);


    String getMaxBatchNoByDate(@Param("date") LocalDate date);

    // 查询总记录数
    @Select("SELECT COUNT(*) FROM furnace_materials")
    Long countAllFuel();

    // 分页查询数据
    @Select("SELECT * FROM furnace_materials LIMIT #{offset}, #{pageSize}")
    List<Fuel> findAllFuel(@Param("offset") int offset, @Param("pageSize") int pageSize);
}
